//
//  XOShopTabBarController.m
//  didigo
//
//  Created by JS_Coder on 2019/2/20.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOShopTabBarController.h"
#import "FlyNavigationController.h"
#import "XOShopViewModel.h"
#import "XOShopModel.h"
@interface XOShopTabBarController()
@property(nonatomic, strong) NSMutableArray * shopChildsArr;

@end

@implementation XOShopTabBarController

- (NSMutableArray *)shopChildsArr {
    if (_shopChildsArr != nil) {
        return _shopChildsArr;
    }
    NSArray *childs = @[@{@"image": @"tabBarItem1", @"title": @"商品", @"controller":@"XOShopHomeViewController"},
                        @{@"image": @"tabBarItem3", @"title": @"购物车", @"controller":@"XOCartViewController"},
                        @{@"image": @"tabBarItem2", @"title": @"商家", @"controller": @"XOShopInfomationController"}
                        ];
    _shopChildsArr = [NSMutableArray arrayWithArray:childs];
    return _shopChildsArr;
}

+ (void)initialize {
        // 设置全局tabBar文字颜色
    UITabBarItem * tabBarItem = [UITabBarItem appearance];
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:kBlackColor, NSForegroundColorAttributeName,kSystemFontSize(12),NSFontAttributeName,nil] forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:AppMainColor, NSForegroundColorAttributeName,kSystemFontSize(12),NSFontAttributeName,nil] forState:UIControlStateHighlighted];
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:AppMainColor, NSForegroundColorAttributeName,kSystemFontSize(12),NSFontAttributeName,nil] forState:UIControlStateSelected];

    [tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.tintColor = AppMainColor;

                         // Do any additional setup after loading the view.
    [self configTabBarItem];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    XOShopViewModel *shopViewM = [XOShopViewModel shared];
    self.selectedIndex = shopViewM.checkShopIndex;

}

#pragma mark - 创建移动搜索按钮

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}


#pragma mark - 设置tabbaritem
- (void)configTabBarItem {
    self.view.backgroundColor = UIColor.whiteColor;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];

    for (int i = 0 ; i < self.shopChildsArr.count; i ++) {
        NSDictionary* dict = self.shopChildsArr[i];
        NSString *imageName = dict[@"image"];
        NSString *titleName = dict[@"title"];
        NSString *contolName = dict[@"controller"];
        Class class = NSClassFromString(contolName);
        UIViewController *vc = [class new];
        vc.title = titleName;

        UIImage* normalImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_normal", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage* selectImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_select", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
              vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: titleName image:normalImage selectedImage:selectImage];
        vc.tabBarItem.tag = i;

        FlyNavigationController *nav = [[FlyNavigationController alloc]initWithRootViewController: vc];
        [self addChildViewController: nav];
    }
}


@end
